
#import "XWEncryptor.h"
#import <CommonCrypto/CommonCrypto.h>
#import <CommonCrypto/CommonDigest.h>
@implementation XWEncryptor
+ (NSString *)xw_AesEncrypt:(NSString *)string withKey:(NSString *)key iv:(NSString *)iv
{
    
    NSString *rslt = @"";
    if(string.length <= 0) return rslt;
    
    NSData *paramData = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    size_t keySize = kCCKeySizeAES128;
    char cKey[keySize + 1];
    bzero(cKey, sizeof(cKey));
    [key getCString:cKey maxLength:sizeof(cKey) encoding:NSUTF8StringEncoding];
    
    size_t ivSize = kCCKeySizeAES128;
    char cIv[ivSize + 1];
    bzero(cIv, sizeof(ivSize));
    [iv getCString:cIv maxLength:sizeof(cIv) encoding:NSUTF8StringEncoding];
    
    size_t dataInSize = paramData.length;
    const char *dataIn = [paramData bytes];
    
    size_t dataOutSize = keySize + ivSize + dataInSize + 1;
    void *dataOut = malloc(dataOutSize);
    bzero(dataOut, dataOutSize);
    
    size_t dataOutLength = 0;
    CCCryptorStatus status = CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, cKey, keySize, cIv, dataIn, dataInSize, dataOut, dataOutSize, &dataOutLength);
    if(status == kCCSuccess)
    {
        NSData *tmpData = [NSData dataWithBytesNoCopy:dataOut length:dataOutLength];
        rslt = [tmpData base64EncodedStringWithOptions:0];
    }
    return rslt;
}

+ (NSString *)xw_AesDecrypt:(NSString *)string withKey:(NSString *)key iv:(NSString *)iv
{
    NSString *rslt = @"";
    if(string.length <= 0) return rslt;
    
    NSData *paramData = [[NSData alloc] initWithBase64EncodedString:string options:0];
    
    size_t keySize = kCCKeySizeAES128;
    char cKey[keySize + 1];
    bzero(cKey, sizeof(cKey));
    [key getCString:cKey maxLength:sizeof(cKey) encoding:NSUTF8StringEncoding];
        
    size_t ivSize = kCCKeySizeAES128;
    char cIv[ivSize + 1];
    bzero(cIv, sizeof(ivSize));
    [iv getCString:cIv maxLength:sizeof(cIv) encoding:NSUTF8StringEncoding];
    
    size_t dataInSize = paramData.length;
    const char *dataIn = [paramData bytes];
    
    size_t dataOutSize = keySize + ivSize + dataInSize + 1;
    void *dataOut = malloc(dataOutSize);
    bzero(dataOut, dataOutSize);
    
    size_t dataOutLength = 0;
    CCCryptorStatus status = CCCrypt(kCCDecrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, cKey, keySize, cIv, dataIn, dataInSize, dataOut, dataOutSize, &dataOutLength);
    if(status == kCCSuccess)
    {
        NSData *tmpData = [NSData dataWithBytesNoCopy:dataOut length:dataOutLength];
        rslt = [[NSString alloc] initWithData:tmpData encoding:NSUTF8StringEncoding] ?: @"";
    }
    return rslt;
}
+ (NSString *)xw_calcStringMD5:(NSString *)srcStr {
    const char* str = [srcStr UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
 
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}
@end


